package com.vimi8.ebb.util;

import net.sf.json.JSONArray;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyang on 2016/9/14.
 */
public class SubStringUtil {
   public static String separateGeoCode(String addressCode,String regionLever){
       int level=Integer.parseInt(regionLever);
       String code=null;
       if(level==2){
            String s=addressCode.substring(0,2);
           code=s+"__00000000";
       }else if(level==3){
            String s=addressCode.substring(0,4);
            code=s+"__000000";
       }else if(level==4){
           String s=addressCode.substring(0,6);
           code=s+"___000";
       }else if(level==5){
           String s=addressCode.substring(0,9);
           code=s+"___";
       }
       return code;
   }
    public static String separateGeoCodeByProduct(String addressCode,String regionLever){
        int level=0;
        String code=null;
        if(regionLever!=null&&!"".equals(regionLever)){
            level=Integer.parseInt(regionLever);
        }
        if(level==2){
            String s=addressCode.substring(0,2);
            code=s+"__________";
        }else if(level==3){
            String s=addressCode.substring(0,4);
            code=s+"________";
        }else if(level==4){
            String s=addressCode.substring(0,6);
            code=s+"______";
        }else if(level==5){
            String s=addressCode.substring(0,9);
            code=s+"___";
        }else if(level==6){
            code=addressCode;
        }
        return code;
    }
    public static List<String> separateListString(String s){
        if(StringUtils.isEmpty(s))return new ArrayList<>();
        JSONArray jsonArray=JSONArray.fromObject(s);
        List<String> list=jsonArray.toList(jsonArray,String.class);
        return list;
    }
}
